package com.dstation.mq.producer;


import com.alibaba.fastjson.JSON;

import com.dstation.domain.MqMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class BrowserMqProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    public void messageSendAsync(String collectTopic, String collectUserSearchTag, String keys, MqMessage message) {

        log.info("发送的rocketmq地址："+rocketMQTemplate.getProducer().getNamesrvAddr()+"            "+rocketMQTemplate.getProducer().getProducerGroup());
//        log.debug("发送的rocketmq地址："+rocketMQTemplate.getProducer().getNamesrvAddr()+"            "+rocketMQTemplate.getProducer().getProducerGroup());

        try {
            if(StringUtils.isNotEmpty(keys)) {
                rocketMQTemplate.asyncSend(collectTopic + ":" + collectUserSearchTag, message, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        log.info("数据采集队列mq消息"+keys+"   发送成功！");
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        throwable.printStackTrace();
                        log.info("数据采集队列mq消息"+keys+"   发送失败！");
                    }
                });
            }else {

            }
        } catch (Exception e) {
            log.error("数据采集发消息异常" + "。 Topic:" +collectTopic+ "，Tag:" +collectUserSearchTag+ "，Message:" + message, e);
        }
    }

}
